import {v4}  from 'uuid'
const CommonUtils = {
  /** 判空 */
  isEmpty: function (obj) {
    return obj == null || obj == "" || obj == undefined
  },
  /** 判非空 */
  isNotEmpty: function (obj) {
    return !this.isEmpty(obj)
  },
  /** 判断是否为函数类型 */
  isFunction: function(obj) {
    if(typeof obj === "function") {
      return true
    }
    return false
  },
  /** 将参数解析为insterSQL语句参数 */
  analysisForInsterSql: function (obj, cols, focus) {
    let data = "";
    let condition = ""
    for(let key  in obj){
      if(cols.indexOf(key) > -1) {
        if(this.isEmpty(obj[key])) {
          if(focus) {
            data +=  "null,";
            condition += (key + ",")
          }else {
            continue;
          }
        }else {
          data += ("'" + obj[key] + "',");
          condition += (key + ",")
        }
      }
    }
    if(this.isNotEmpty(data) && this.isNotEmpty(condition)) {
      data = data.substr(0, data.length - 1);
      condition = condition.substr(0, condition.length - 1);
    }else {
      return false
    }
    return {"data": data, "condition": condition}
  },
  /** 将参数解析为updateSQL语句参数 */
  analysisForUpdateSql: function (obj, cols, focus) {
    let data = "";
    for(let key  in obj){
      if(cols.indexOf(key) > -1) {
        if (this.isEmpty(obj[key])) {
          if (focus) {
            data += (key + "= null,");
          } else {
            continue;
          }
        } else {
          data += (key + "='" + obj[key] + "',");
        }
      }
    }
    if(this.isNotEmpty(data)) {
      data = data.substr(0, data.length - 1);
    }
    return data
  },
  /** 清空参数 */
  clearObj: function (obj, keys) {
    let paramNum = arguments.length;
    // 一个参数，清空对象所有值
    if (paramNum < 2) {
      for(let key  in obj){
        obj[key] = null;
      }
    } else {
      // 否则清空指定属性
      let keysArr = keys.split(',');
      for(let key  in keysArr){
        // 有对应的属性则清空
        if (obj.hasOwnProperty(keysArr[key])) {
          obj[keysArr[key]] = null;
        }
      }
    }
  },
  /** 生成uuid */
  uuid: function () {
    return v4()
  },
  /** 手机号验证 */
  validateMobile: function (tel) {
    const pattern = /^1(3|4|5|6|7|8|9)\d{9}$/
    return pattern.test(tel);
  }

}
export default CommonUtils